﻿using UnityEngine;
using System.Collections;

public class Enemy2 : MonoBehaviour {
	
	//生命
	int m_Life = 10;
	//速度
	float m_speed =1;
	//发射子弹的时间
	protected float fashe_Time =1;
	//玩家
	GameObject m_Player;
	Transform m_Transform;
	
	void Start () {
		
		m_Transform = this.transform;
		m_Player = GameObject.FindGameObjectWithTag("Player");
	}
	
	void Update () {
		//this.UpdataMove();
		fasheRocket();
	}
	
	void UpdataMove()
	{
		float m_Y = m_Transform.position.y;
		m_Transform.position = new Vector3(m_Transform.position.x,m_Y-=Time.deltaTime*m_speed,0);
	}
	
	//发射子弹
	void fasheRocket()
	{
		fashe_Time-=Time.deltaTime;
		if(fashe_Time<=0)
		{
			Vector2 relativePos =new Vector2(Mathf.Sqrt(3),1) - new Vector2(Mathf.Sqrt(12),2);
			Quaternion Rotation = Quaternion.identity; 
			Rotation.eulerAngles = new Vector3(0,0,150);
			Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[10].ToString()),new Vector3(m_Transform.position.x,m_Transform.position.y - 0.5F,0),Rotation);
			Rotation.eulerAngles = new Vector3(0,0,210);
			Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[10].ToString()),new Vector3(m_Transform.position.x,m_Transform.position.y - 0.5F,0),Rotation);
			fashe_Time = 2;
		}
	}
	
	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.tag.CompareTo("PlayerRocket1") == 0)
		{
            Rocket1 rocket1 = other.GetComponent<Rocket1>();
            if (rocket1 != null)
            {
                m_Life-=rocket1.rocket_Power;
                Destroy(rocket1.gameObject);
            }
			if(m_Life<=0)
			{
				this.SiWangTeXiao();
			}
        }
        if (other.tag.CompareTo("PlayerRocket2") == 0)
        {
            Rocket2 rocket2 = other.GetComponent<Rocket2>();
            if (rocket2 != null)
            {
                m_Life -= rocket2.rocket_Power;
                Destroy(rocket2.gameObject);
            }
            if (m_Life <= 0)
            {
				this.SiWangTeXiao();
            }

		}
        if(other.tag.CompareTo("PlayerRocket3") == 0)
		{
			Rocket3 rocket3 = other.GetComponent<Rocket3>();
			if(rocket3!=null)
			{
				m_Life -= rocket3.rocket_power;
				Destroy(rocket3.gameObject);
				if (m_Life <= 0)
				{
					this.SiWangTeXiao();	
				}
			}
		}
        if (other.tag.CompareTo("HeDan") == 0 || other.tag.CompareTo("Player") == 0)
		{
			this.SiWangTeXiao();
		}
		if(other.tag.CompareTo("Bounds") == 0){
			Destroy(this.gameObject);
		}
		
	}

	//出现钻石
	protected void SiWangTeXiao()
	{
		Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[11].ToString()), m_Transform.position, Quaternion.identity);
		Instantiate(Resources.Load<GameObject>(SampleXml.prefabPath[19].ToString()),m_Transform.position,Quaternion.identity);

		int number =0;
		number = Random.Range(5,9);
		GameObject obj_ZuanShi = Resources.Load<GameObject>(SampleXml.prefabPath[5].ToString());
		for(int i=0;i<number;i++)
		{
			Instantiate(obj_ZuanShi,this.ZuanShiPosition(m_Transform.position),Quaternion.identity);
		}
		Destroy(this.gameObject);
	}

	//金币移动出现
	protected Vector3 ZuanShiPosition(Vector3 vec)
	{
		Vector3 vec3 = new Vector3(Random.Range(vec.x-1,vec.x+1),Random.Range(vec.y-1,vec.y+1),0);
		return vec3;
	}
}
